package com.mobileduty.pages;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.Button;

import com.andoridtools.utils.SPUtils;
import com.mobileduty.R;
import com.mobileduty.adapter.GuideViewPagerAdapter;
import com.mobileduty.comm.Constants;
import com.mobileduty.entry.GuildeBean;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;

/**
 * 引导界面
 */

public class GuideActivity extends Activity implements View.OnClickListener {
    private Context context = this;

    private Button guide_go;

    private ViewPager guide_viewpager;
    private RecyclerView guide_recyler;

    private ArrayList<Integer> viewpagerArrayList;
    private GuideViewPagerAdapter guideViewPagerAdapter;


    private ArrayList<GuildeBean> pointArrayList;
    private CommonAdapter<GuildeBean> viewpagerAdapter;
    private CommonAdapter<GuildeBean> pointAdapter;
    private int size = 3;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.guide_main);
        initView();
    }

    private void initView() {
        guide_go = (Button) findViewById(R.id.guide_go);

        guide_viewpager = (ViewPager) findViewById(R.id.guide_viewpager);
        viewpagerArrayList = new ArrayList<>();
        viewpagerArrayList.add(R.mipmap.guide1);
        viewpagerArrayList.add(R.mipmap.guide2);
        viewpagerArrayList.add(R.mipmap.guide3);

        guideViewPagerAdapter = new GuideViewPagerAdapter(context, viewpagerArrayList);
        guide_viewpager.setAdapter(guideViewPagerAdapter);
        guide_viewpager.addOnPageChangeListener(onPageChangeListener);

        guide_recyler = (RecyclerView) findViewById(R.id.guide_recyler);
        pointArrayList = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            GuildeBean guildeBean = new GuildeBean();
            if (i == 0) {
                guildeBean.select = true;
            } else {
                guildeBean.select = false;
            }
            pointArrayList.add(guildeBean);
        }


        guide_recyler.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false));
        pointAdapter = new CommonAdapter<GuildeBean>(context, R.layout.guide_item_point, pointArrayList) {
            @Override
            protected void convert(ViewHolder holder, GuildeBean guildeBean, int position) {
                if (guildeBean.select) {
                    holder.setBackgroundRes(R.id.guide_point, R.drawable.ic_guide_point_round_select);
                } else {
                    holder.setBackgroundRes(R.id.guide_point, R.drawable.ic_guide_point_round_unselect);
                }
            }
        };
        guide_recyler.setAdapter(pointAdapter);

        guide_go.setOnClickListener(this);
    }

    //切换小红点
    public void select(int index) {
        for (int i = 0; i < size; i++) {
            GuildeBean guildeBean = pointArrayList.get(i);
            if (i == index) {
                guildeBean.select = true;
            } else {
                guildeBean.select = false;
            }
        }
        pointAdapter.notifyDataSetChanged();
    }


    //监听viewpager
    ViewPager.OnPageChangeListener onPageChangeListener = new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {
            select(position);
            if (position == size - 1) {
                guide_go.setVisibility(View.VISIBLE);
            } else {
                guide_go.setVisibility(View.GONE);
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    };

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.guide_go:
                //记录已经打开过了
                SPUtils.put(context, Constants.FIRSTOPEN,false);

                startActivity(new Intent(context,LoginActivity.class));

                finish();
                break;
        }
    }
}
